//
//  ViewController.m
//  Vegetables
//
//  Created by hpkj on 2020/1/17.
//  Copyright © 2020 Vegetables. All rights reserved.
//

#import "ViewController.h"
#import "VegetableManager.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableView;

@property (nonatomic,weak) UIAlertController *alertController;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.listTableView.dataSource = self;
    self.listTableView.delegate = self;
    
    UIBarButtonItem * rightBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"增加" style:(UIBarButtonItemStylePlain) target:self action:@selector(addVegetables)];
    [self.navigationItem setRightBarButtonItem:rightBarBtn]
    ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[VegetableManager sharedInstance] vegetables].count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Vegetable * vegModel = [[[VegetableManager sharedInstance] vegetables] objectAtIndex:indexPath.row];
    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
    cell.textLabel.text = vegModel.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"￥%.2f",vegModel.price];
    return cell;
}

-(void)addVegetables
{
//    生成提示框
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"增加蔬菜" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    _alertController = alertController;
    
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"蔬菜名";
        textField.tag = 1;
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"菜价";
        textField.tag = 2;
    }];
    UIAlertAction * confirmAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSArray * textFields = _alertController.textFields;
        NSString * vegName = nil;
        double vegPrice = 0;
        for (UITextField * tf in textFields) {
            if (tf.tag == 1) {
                vegName = tf.text;
            }else if (tf.tag == 2)
            {
                vegPrice = [tf.text doubleValue];
            }
        }
        
        Vegetable * vegModel = [[Vegetable alloc] init];
        vegModel.name = vegName;
        vegModel.price = vegPrice;
        [[VegetableManager sharedInstance] addvegetable:vegModel];
        [_listTableView reloadData];
        _alertController = nil;
    }];
    
    [alertController addAction:confirmAction];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}


-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//}



- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
}



- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)){
    if (@available(iOS 11.0, *)) {
        
        
        
        UIContextualAction * addRowAction = [UIContextualAction contextualActionWithStyle:(UIContextualActionStyleNormal) title:@"加一块钱" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"操作提醒" message:@"确定要加1块钱吗？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                NSLog(@"加一块钱");
                
                Vegetable *vegModel = [[VegetableManager sharedInstance].vegetables objectAtIndex:indexPath.row];
                [[VegetableManager sharedInstance] changePrice:vegModel.price + 1 Name:vegModel.name];
                [self.listTableView reloadData];
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:action1];
            [alertController addAction:action2];
            
            //显示alertController
            [self presentViewController:alertController animated:YES completion:nil];
        }];
        
        
        
        UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"操作提醒" message:@"确定要删除该记录吗？" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                NSLog(@"删除了这条记录");

                Vegetable *vegModel = [[VegetableManager sharedInstance].vegetables objectAtIndex:indexPath.row];
                [[VegetableManager sharedInstance] removeVegetableWihtName:vegModel.name];
                [self.listTableView reloadData];

                
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:action1];
            [alertController addAction:action2];
            
            //显示alertController
            [self presentViewController:alertController animated:YES completion:nil];
        }];
        
        UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,addRowAction]];
        config.performsFirstActionWithFullSwipe = NO;
        return config;
    } else {
        // Fallback on earlier versions
        return nil;
    }

}



@end
    
    
